using System;
using Microsoft.SharePoint;

namespace arb.SharePoint.SecureSilverlight
{
    public class HandleGroupCheckEvent : SPItemEventReceiver
    {
        public override void ItemUpdating(SPItemEventProperties properties)
        {
            try
            {
                DisableEventFiring();
                using (var site = new SPSite(properties.SiteId))
                {
                    bool userIsAdmin;
                    using (SPWeb web = site.OpenWeb())
                    {
                        userIsAdmin = web.CurrentUser.IsMemberOfGroup("Silverlight Administrators");
                    }
                    if (userIsAdmin)
                    {
                        base.ItemUpdating(properties);
                    }
                    else
                    {
                        properties.ErrorMessage = string.Format("You Must Be A Member Of The Silverlight Administrators Group To Update This Item.");
                        properties.Status = SPEventReceiverStatus.CancelWithError;
                        properties.Cancel = true;
                    }
                }
            }
            catch (Exception ex)
            {
                properties.ErrorMessage = string.Format("An error occurred during execution of ItemAdding event: {0}.", ex.Message);
                properties.Status = SPEventReceiverStatus.CancelWithError;
                properties.Cancel = true;
            }
            EnableEventFiring();
        }
    }
}
